package com.vvlive.caibao.smartcity.view;

import android.content.Context;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.View;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;

import com.vvlive.caibao.smartcity.bean.WeekCell;
import com.vvlive.caibao.smartcity.bean.WeekRow;
import com.vvlive.caibao.smartcity.utils.ScreenUtil;

import java.util.ArrayList;
import java.util.List;

import smartcity.vvlive.caibao.com.smartc.R;

/**
 * Created by zhj on 2015/10/12.
 * 日历周视图
 * +----+----+----+----+----+----+----+----+
 * | || |    |    |    |    |    | -- | -- |
 * +----+----+----+----+----+----+----+----+
 * | || |    |    |    |    |    | -- | -- |
 * +----+----+----+----+----+----+----+----+
 * | || |    |    |    |    |    | -- | -- |
 * +----+----+----+----+----+----+----+----+
 * | || |    |    |    |    |    | -- | -- |
 * +----+----+----+----+----+----+----+----+
 * | || |    |    |    |    |    | -- | -- |
 * +----+----+----+----+----+----+----+----+
 */
public class WeekView extends TableLayout implements View.OnClickListener {

    private static final int WEEK_ROW_RES = R.layout.view_week_row;
    private static final int SELECTED_COLOR = Color.parseColor("#FE716A");
    private static final int CELL_HEIGHT = 96;

    private static final int VISIBLE_CELL_SIZE = 6;
    private static final int FINAL_CELL_SIZE = 8;

    private OnCellClickListener onCellClickListener;
    private OnClassifyClickListener onClassifyClickListener;
    private ScreenUtil mScreen;
    private int mCellHeight;
    private int mCellWidth;
    private int mTargetViewWidth;
    private String[] header;

    public WeekView(Context context) {
        this(context, null);
    }

    public WeekView(Context context, AttributeSet attrs) {
        super(context, attrs);
        mScreen = new ScreenUtil(context);
        mCellHeight = (int) mScreen.dip2px(CELL_HEIGHT);
        mCellWidth = getCellWidth();
        mTargetViewWidth = getTargetViewWidth();
        this.setMinimumWidth(mTargetViewWidth);
    }

    /**
     * 添加头部
     *
     * @param headerRow
     */
    public void addHeader(String[] headerRow) {
        header = headerRow;
        if (getChildCount() == 0) {
            List<WeekCell> weekCells = new ArrayList<>();
            for (String rowText : headerRow) {
                weekCells.add(new WeekCell(rowText, false));
            }
            addRow(new WeekRow(weekCells, false));
        }
    }

    /**
     * 添加一行数据
     *
     * @param weekRow
     */
    public void addRow(WeekRow weekRow) {

        TableRow rowLayout = (TableRow) inflate(getContext(), WEEK_ROW_RES, null);
        List<WeekCell> weekCells = weekRow.getWeekCells();
        int cellsSize = weekCells.size();

        for (int i = 0; i < cellsSize; i++) {

            WeekCell weekCell = weekCells.get(i);
            TextView cell = ((TextView) rowLayout.getChildAt(i));
            String text = weekCell.getText();
            int pos = text.indexOf("(");
            if (pos > -1) {
                String[] temp = text.split("\\(");
                text = temp[0] + "\n(" + temp[1];
                text = text.substring(0, pos - 1) + "\n" + text.substring(pos - 1);
            }
            cell.setText(text);
            if (weekCell.isChecked()) {
                cell.setBackgroundColor(SELECTED_COLOR);
            }

            int row = this.getChildCount() - 1;
            int col = i - 1;
            if (row > -1 && col > -1) {
                weekCell.setRow(row);
                weekCell.setCol(col);
                cell.setTag(weekCell);
                cell.setMinHeight(mCellHeight);
                cell.setOnClickListener(this);
            } else if (row == -1 && col == -1) {
//                cell.setBackgroundColor(SELECTED_COLOR);
                cell.setOnClickListener(this);
            }

        }

        this.addView(rowLayout);

    }

    /**
     * 替换一个空格
     *
     * @param target
     * @param row
     * @param col
     */
    public void replaceCell(View target, int row, int col) {
        TableRow tableRow = (TableRow) this.getChildAt(row + 1);
        int replaceIndex = col + 1;
        View viewRemoved = tableRow.getChildAt(replaceIndex);
        target.setLayoutParams(viewRemoved.getLayoutParams());
        tableRow.removeView(viewRemoved);
        tableRow.addView(target, replaceIndex);
        WeekCell weekCell = new WeekCell();
        weekCell.setRow(row);
        weekCell.setCol(col);
        weekCell.setIsChecked(false);
        target.setTag(weekCell);
        target.setOnClickListener(this);
    }

    public void addRows(List<WeekRow> weekRows) {
        for (WeekRow row : weekRows) {
            addRow(row);
        }
    }

    /**
     * 重置每一行的数据
     *
     * @param weekRows
     */
    public void resetRows(List<WeekRow> weekRows) {
        this.removeAllViews();
        addHeader(header);
        addRows(weekRows);
    }

    @Override
    public void onClick(View v) {
        Object tag = v.getTag();
        if (tag != null) {
            if (onCellClickListener != null) {
                onCellClickListener.onCellClick(v, (WeekCell) tag);
            }
        } else {
            if (onClassifyClickListener != null) {
                onClassifyClickListener.onClassifyClick();
            }
        }
    }

    /**
     * 设置唯一显示的哪一行
     *
     * @param row
     */
    public void setOnlyShowRow(int row) {
        for (int i = 1, count = getChildCount(); i < count; i++) {
            if(i != row + 1) {
                getChildAt(i).setVisibility(GONE);
            }
        }
//        getChildAt(row + 1).setVisibility(VISIBLE);
    }

    private int getCellWidth() {
        return mScreen.getWidth() / VISIBLE_CELL_SIZE;
    }

    private int getTargetViewWidth() {
        return getCellWidth() * FINAL_CELL_SIZE;
    }

    public interface OnCellClickListener {
        void onCellClick(View view, WeekCell cell);
    }

    /**
     * 设置每一个空格的点击事件
     *
     * @param onCellClickListener
     */
    public void setOnCellClickListener(OnCellClickListener onCellClickListener) {
        this.onCellClickListener = onCellClickListener;
    }

    public interface OnClassifyClickListener {
        void onClassifyClick();
    }

    public void setOnClassifyClickListener(OnClassifyClickListener onClassifyClickListener) {
        this.onClassifyClickListener = onClassifyClickListener;
    }
}
